<!DOCTYPE HTML>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%
	// Create a factory for disk-based file items
	DiskFileItemFactory factory = new DiskFileItemFactory();

	// Configure a repository (to ensure a secure temp location is used)
	ServletContext servletContext = this.getServletConfig()
			.getServletContext();
	File repository = (File) servletContext
			.getAttribute("javax.servlet.context.tempdir");
	factory.setRepository(repository);

	// Create a new file upload handler
	ServletFileUpload upload = new ServletFileUpload(factory);

	// Parse the request
	List<FileItem> items = upload.parseRequest(request);

	//Process the uploaded items
	Iterator<FileItem> iter = items.iterator();
	while (iter.hasNext()) {
		FileItem item = iter.next();

		if (item.isFormField()) {
			//processFormField(item);
		} else {
			pageContext.setAttribute("inputStream",
					item.getInputStream());
		}
	}
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>Fuel Program</title>
<!-- Bootstrap -->
<link href="/FuelProgram/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
	<div class="container">
		<c_rt:if test="${empty gasStations }">
			<jsp:useBean id="bestRouteSuggestor"
			class="com.alphagen.sample.bestroute.BestRouteSuggester"
			scope="session">
			<%-- intialize bean properties --%>
			<jsp:setProperty name="bestRouteSuggestor" property="inputFile"
				value="${inputStream }" />
			</jsp:useBean>
			<c_rt:if test="${fn:length(bestRouteSuggestor.parsedInput) > 0 }">
				<c_rt:set var="gasStations" value="${bestRouteSuggestor.parsedInput }"
				scope="session" />
				<c:redirect url="/ShowStationsInfo"/>
			</c_rt:if>		
		</c_rt:if>
		<c_rt:if test="${not empty gasStations }">
			<c:redirect url="/ShowStationsInfo"/>
		</c_rt:if>
		${fn:length(bestRouteSuggestor.parsedInput) } Gas station(s) records loaded
		
			
		<a href="/FindRoute">Find Route between Destinations</a>
	</div>

	<script
		src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
	<!-- Include all compiled plugins (below), or include individual files as needed -->
	<script src="js/bootstrap.min.js"></script>
</body>
</html>
